package com.area.cth.service.impl;

import com.area.cth.mapper.CommentMapper;
import com.area.cth.pojo.CommentGoods;
import com.area.cth.pojo.CommentPost;
import com.area.cth.common.PageBean;
import com.area.cth.service.CommentService;
import com.area.cth.utils.ThreadLocalUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, CommentPost> implements CommentService {
    @Autowired
    private CommentMapper cm;

    @Override
    public void addCommentToPost(CommentPost c) {
        c.setCreateTime(LocalDateTime.now());
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        c.setCommentUser(id);
        cm.addCommentToPost(c);
    }

    @Override
    public void addCommentToGoods(CommentGoods c) {
        c.setCreateTime(LocalDateTime.now());
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        c.setCommentUser(id);
        cm.addCommentToGoods(c);
    }

    @Override
    public List<CommentPost> getCommentPostList(Integer pId) {
        return cm.getCommentPostList(pId);
    }

    @Override
    public List<CommentGoods> getCommentGoodsList(Integer gId) {
        return cm.getCommentGoodsList(gId);
    }

    @Override
    public Integer postCommentCount(Integer pId) {
        return cm.postCommentCount(pId);
    }

    @Override
    public List<CommentPost> commentPostListForMine() {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        return cm.commentPostListForMine(id);
    }

    @Override
    public List<CommentGoods> commentGoodsListForMine() {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        return cm.commentGoodsListForMine(id);
    }

    @Override
    public void deletePost(Integer id) {
        cm.deletePost(id);
    }

    @Override
    public void deleteGoods(Integer id) {
        cm.deleteGoods(id);
    }

    @Override
    public PageBean<CommentPost> commentPostList(Integer pageNo, Integer pageSize) {
        Page<CommentPost> page=Page.of(pageNo, pageSize);
        Page<CommentPost> p=lambdaQuery()
                .page(page);
        PageBean<CommentPost> pb=new PageBean<>();
        pb.setTotal(p.getTotal());
        pb.setPages(p.getPages());
        pb.setRecords(p.getRecords());
        return pb;
    }

//    @Override
//    public IPage<CommentGoods> commentGoodsList(Integer pageNo, Integer pageSize) {
//        return null;
//    }
}
